package com.lukk.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lukk.pojo.PotentialWorker;
import com.lukk.pojo.Worker;
import com.lukk.service.IPotentialWorkerService;
import com.lukk.service.IWorkerService;
import com.lukk.util.HttpRequest;
import com.lukk.util.JsonUtil;
import com.lukk.util.jpush.JSMSUtil;

/**
 * 
 * WorkerController 上午11:27:18 Copyright zhaocj Inc. All Rights reserved.
 * alldios@outlook.com
 *
 */
@Controller
@RequestMapping("/worker")
public class WorkerController {
	private static Logger logger = LogManager.getLogger(WorkerController.class.getName());

	@Autowired
	private IWorkerService workerService;

	public IWorkerService getWorkerService() {
		return workerService;
	}

	public void setWorkerService(IWorkerService workerService) {
		this.workerService = workerService;
	}

	@Autowired
	private IPotentialWorkerService potentialWorkerService;

	public IPotentialWorkerService getPotentialWorkerService() {
		return potentialWorkerService;
	}

	public void setPotentialWorkerService(IPotentialWorkerService potentialWorkerService) {
		this.potentialWorkerService = potentialWorkerService;
	}

	// 解析视图
	@RequestMapping("/verifyingWorker")
	public String verifyingWorker(Model model) {
		model.addAttribute("oneMenu", "verifyWorker");
		model.addAttribute("twoMenu", "verifyingWorker");
		return "/cms/verifyWorker/verifyingWorker";
	}

	// 解析视图
	@RequestMapping("/verifiedWorker")
	public String verifiedWorker(Model model) {
		model.addAttribute("oneMenu", "verifyWorker");
		model.addAttribute("twoMenu", "verifiedWorker");
		return "/cms/verifyWorker/verifiedWorker";
	}
	// 解析视图
	@RequestMapping("/countWorker")
	public String countWorker(Model model) {
		model.addAttribute("oneMenu", "verifyWorker");
		model.addAttribute("twoMenu", "countWorker");
		return "/cms/verifyWorker/countWorker";
	}

	// 解析视图
	@RequestMapping("/workerCensus")
	public String workerCensus(Model model) {
		model.addAttribute("oneMenu", "verifyWorker");
		model.addAttribute("twoMenu", "workerCensus");
		return "/cms/verifyWorker/workerCensus";
	}

	// 解析视图
	@RequestMapping("/viWorkerWDrawal")
	public String verifyingWithDrawal(Model model) {
		model.addAttribute("oneMenu", "verifyWithDrawal");
		model.addAttribute("twoMenu", "viWorkerWDrawal");
		return "/cms/verifyWithDrawal/viWorkerWDrawal";
	}

	// 解析视图
	@RequestMapping("/vdWorkerWDrawal")
	public String verifedWithDrawal(Model model) {
		model.addAttribute("oneMenu", "verifyWithDrawal");
		model.addAttribute("twoMenu", "vdWorkerWDrawal");
		return "/cms/verifyWithDrawal/vdWorkerWDrawal";
	}

	// 查询page 主要用于查看还未验证的客户
	@RequestMapping("/queryByProvinceAndName")
	public @ResponseBody Map<String, Object> selectByProvinceAndName(Integer offset, Integer limit, String sort,
			String order, String beginTime, String endTime, Integer status, String provinceCode, String workerName,
			Integer isDisable, String phoneNumber, HttpSession session) {
		if ("0".equals(provinceCode)) {
			logger.debug("查询的是全部省份的员工的信息");
			return workerService.selectAllProvinceWorkersForInternalWorker(offset, limit, sort, order, beginTime,
					endTime, status, provinceCode, workerName, phoneNumber, isDisable, session);
		}
		return workerService.selectByProvinceAndName(offset, limit, sort, order, beginTime, endTime, status,
				provinceCode, workerName, phoneNumber, isDisable);

	}

	// 查询page 主要用于查看验证已经通过的页面
	@RequestMapping("/queryByProvinceAndStatus")
	public @ResponseBody Map<String, Object> queryByProvinceAndStatus(Integer offset, Integer limit, String sort,
			String order, String provinceCode, String workerName, String typeValue, String phoneNumber,
			String queryType, HttpSession session) {
		logger.debug(
				"收到的查询条件是offset:{}，limit:{}，sort:{}，order:{}，provinceCode:{}，workerName:{}，typeValue:{}，phoneNumber:{}，queryType:{}",
				offset, limit, sort, order, provinceCode, workerName, typeValue, phoneNumber, queryType);
		return workerService.selectByProvinceAndStatus(offset, limit, sort, order, provinceCode, workerName, typeValue,
				phoneNumber, queryType, session);
	}

	// 修改员工的状态 只需要两个值 一个是员工的ID值 一个是员工的状态值
	@RequestMapping("/updateWorkerStatus")
	public @ResponseBody boolean updateWorkerStatus(Worker worker, Integer messageType) {
		// 如果通过的话 会发送短信给通过客户
		if (worker.getStatus() == 1) {
			String s = HttpRequest.sendGet("http://v1.avatardata.cn/Sms/Send",
					"key=d4bc2afdcc6d4231bd8f793d9ca6d694&mobile=" + worker.getPhone()
							+ "&templateId=b9fe7b3b20c14a3283c622a1e0c670b0");
			// 通过后会删除lukk_internalWorker表中的数据 并会查询到录入员工的ID
			Integer internalWorkerID = potentialWorkerService.deleteByPhone(worker.getPhone());
			worker.setInternalWorkerID(internalWorkerID);
		} else {
			// 没有通过之后会发送给客户不同的为什么不通过的原因
			String message = "";
			if (messageType == 0) {
				message = "头像不符合要求";
			} else if (messageType == 1) {
				message = "姓名不符合要求";
			} else if (messageType == 2) {
				message = "工种不匹配";
			} else {
				message = "头像和姓名未通过";
			}
			String s = HttpRequest.sendGet("http://v1.avatardata.cn/Sms/Send",
					"key=d4bc2afdcc6d4231bd8f793d9ca6d694&mobile=" + worker.getPhone()
							+ "&templateId=cc210b5f82214eef845e632a83271401&param=" + message);
			PotentialWorker potentialWorker = new PotentialWorker();
			potentialWorker.setPhoneNumber(worker.getPhone());
			potentialWorker.setStatus((char) 2);
			potentialWorkerService.modifyByBean(potentialWorker);
		}
		return workerService.modifyByPrimaryKey(worker);
	}

	// 修改员工的状态 只需要两个值 一个是员工的ID值 一个是员工的状态值
	@RequestMapping("/updateWorkerDisabledStatus")
	public @ResponseBody boolean updateWorkerDisabledStatus(Worker worker) {
		logger.debug("修改用户的ID{},修改状态{}" + worker.getWorkerId() + " || " + worker.getIsDisable());
		workerService.modifyByPrimaryKey(worker);
		return true;
	}
	
	//修改员工权限状态
	@RequestMapping("/updateWorkerRightStatus")
	public @ResponseBody boolean updateWorkerRightStatus(Worker worker) {
		logger.debug("要修改的worker的值：{}", JsonUtil.toJson(worker));
		workerService.modifyByPrimaryKey(worker);
		System.out.println("===============updateWorkerRightStatus");
		return true;
	}

	@RequestMapping("/sendMessage")
	public @ResponseBody boolean sendMessageToWorkers(String workers) {
		String[] split = workers.split(",");
		for (String string : split) {
			Worker bean = workerService.selectByPrimaryKey(Integer.valueOf(string));
			JSMSUtil.sendPromoteMessageToUser(bean.getPhone());
		}
		return true;
	}

	/**
	 * 根据省份，城市，区域查询对应的注册工人信息
	 * 
	 * @param queryType
	 * @param provinceCode
	 * @param limit
	 * @param offset
	 * @return
	 */
	@RequestMapping("/queryWorkersByProvinceCode")
	public @ResponseBody Map<String, Object> queryWorkersByProvinceCode(Integer queryType, String provinceCode,
			Integer limit, Integer offset) {
		logger.debug("想要查询工人的城市的code是：{},类型：{}", provinceCode, queryType);
		return workerService.selectWorkersByProvinceCode(queryType, provinceCode, limit, offset);
	}

	/**
	 * 根据省份和其他信息对工人绑定的提现信息进行审核，展示筛选结果不进行排序
	 * 
	 * @param offset
	 * @param limit
	 * @param beginTime
	 * @param endTime
	 * @param status
	 * @param provinceCode
	 * @param workerName
	 * @param phoneNumber
	 * @param session
	 * @return
	 */
	@RequestMapping("/queryWorkerWithDrawalInfo")
	public @ResponseBody Map<String, Object> queryWorkerWithDrawalInfo(Integer offset, Integer limit, String beginTime,
			String endTime, Integer withdrawalStatus, String provinceCode, String workerName, String phoneNumber,
			HttpSession session) {
		return workerService.selectWorkerWithDrawalInfo(offset, limit, beginTime, endTime, withdrawalStatus,
				provinceCode, workerName, phoneNumber, session);
	}

	/**
	 * 
	 * @param worker
	 *            根据worker的ID对该员工的可提现状态进行修改
	 * @return
	 */
	@RequestMapping("/updateWorkerWithDrawalStatus")
	public @ResponseBody boolean updateWorkerWithDrawalStatus(Worker worker) {
		logger.debug("要修改的worker的值：{}", JsonUtil.toJson(worker));
		workerService.modifyByPrimaryKey(worker);
		JSMSUtil.sendVerifyWithdrawal(worker.getPhone(), worker.getWithdrawalStatus());
		return true;
	}

	/**
	 * 
	 * @param worker
	 *            根据worker的ID对该员工的可提现状态进行修改
	 * @return
	 */
	@RequestMapping("/queryByPrimaryKey")
	public @ResponseBody Worker queryByPrimaryKey(Integer workerID) {
		logger.debug("要查询的worker的ID值：{}", workerID);
		return workerService.selectByPrimaryKey(workerID);
	}
	/**
	 * 
	 * @param worker
	 *            根据worker的ID查询该工人的权限
	 * @return
	 */
	@RequestMapping("/queryAllocatedReduce")
	public @ResponseBody List<Integer> queryAllocatedReduce(String workerID) {
		logger.debug("查询者的ID是：{}", workerID);
		return workerService.selectAllocatedReduce(workerID);
	}
	
	
	
	
}
